<?php
/**
 * User zgh
 * Email 1803897171@qq.com
 * Date 2021/7/19
 **/

namespace app\admin\controller;


use app\admin\service\MenuService;
use app\common\Annotation\ControllerAnnotation;
use app\common\Annotation\NodeAnotation;
use app\common\controller\BackendController;
use think\facade\Request;

/**
 * Class MenuController
 * @ControllerAnnotation(title="菜单管理",auth=2)
 * @package app\admin\controller
 */
class MenuController extends BackendController
{

    protected function initialize()
    {
        parent::initialize();
        $menu_data = MenuService::getMenuAll(0, 0);
        $pidMenuList = [];
        MenuService::getModule($menu_data, $pidMenuList);
        $this->assign("pidMenuList", $pidMenuList);
    }

    /**
     * @NodeAnotation(title="列表")
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if (Request::isAjax()) {
            $res = MenuService::getList(Request::param());
            return success_json_response("成功", $res['dataList'], ['count' => $res['count']]);
        } else {
            return $this->fetch();
        }
    }

    /**
     * @NodeAnotation(title="添加")
     * @return mixed|\think\response\Json
     * @throws \app\common\exception\ServiceException
     */
    public function add()
    {
        $params = Request::param();
        if (Request::isAjax()) {
            $res = MenuService::save($params);
            if ($res) {
                return success_json_response("修改成功", ["id" => $res]);
            } else {
                return json_response("修改失败");
            }
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="修改")
     * @return mixed|\think\response\Json
     * @throws \app\common\exception\ServiceException
     */
    public function edit()
    {
        $params = Request::param();
        if (Request::isAjax()) {
            $res = MenuService::save($params);
            return $res ? success_json_response("修改成功", ["id" => $res]) : json_response("修改失败");
        }
        $model = MenuService::getByID($params['id']);
        $this->assign("model", $model);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除")
     * @return \think\response\Json
     * @throws \app\common\exception\ServiceException
     */
    public function del()
    {
        if (Request::isAjax()) {
            $res = MenuService::del(Request::param());
            return $res ? success_json_response("删除成功") : json_response("删除失败");
        }
    }


}